package debby.bdmap

import android.Manifest
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.baidu.location.LocationClient
import com.baidu.mapapi.SDKInitializer
import com.gyf.barlibrary.ImmersionBar
import debby.baidumap.R
import debby.bdmap.interfaceutil.MainUtils
import debby.bdmap.utils.*
import kotlinx.android.synthetic.main.activity_main.*
import pub.devrel.easypermissions.EasyPermissions
import android.content.Intent
import debby.bdmap.capture.MCaptureActivity
import org.jetbrains.anko.*


class MainActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks, MainUtils {


    private val sCAN = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        SDKInitializer.initialize(applicationContext)
        initTitleBar(true)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        initBDUi(map.map.uiSettings)

        bt_change.setOnClickListener { changeMap(map.map) }
        bt_traffic.setOnClickListener { showTraffic(map.map) }
        bt_heat.setOnClickListener { showHeat(map.map) }
        bt_poi.setOnClickListener { showPoi(map.map) }
        iv_location.setOnClickListener { location() }
        iv_clear.setOnClickListener { clearOverlay(this) }
        iv_revoke.setOnClickListener { revoke(map.map, this) }
        //扫描
        bt_scan.setOnClickListener { scan() }

        initMap(map.map, this)
        drawInfoWindow(map.map, applicationContext)
    }

    //定位
    private fun location() {
        if (EasyPermissions.hasPermissions(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
            initLocation(LocationClient(applicationContext), map.map)
        } else {
            EasyPermissions.requestPermissions(this, "请求定位权限",
                    10010, Manifest.permission.ACCESS_FINE_LOCATION)
        }
    }

    //运行时权限
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode,
                permissions, grantResults, this)
    }

    //请求拒绝
    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        when (requestCode) {
            10010 -> toast("需要权限才可以定位")
            10086 -> toast("拍照权限给我")
        }
    }

    //请求通过
    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
        when (requestCode) {
            10010 -> initLocation(LocationClient(applicationContext), map.map)
            10086 -> jumpScanPage()
        }
    }

    override fun onResume() {
        super.onResume()
        map.onResume()
        location()
    }

    override fun onPause() {
        super.onPause()
        map.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        map.onDestroy()
        initTitleBar(false)
    }

    override fun toast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }

    override fun setDistance(distance: String) {
        tv_distance.text = distance
    }

    private fun scan() {
        if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) {
            jumpScanPage()
        } else {
            EasyPermissions.requestPermissions(this, "请求拍照",
                    10086, Manifest.permission.CAMERA)
        }
    }

    private fun jumpScanPage() {
        startActivityForResult(Intent(this@MainActivity,
                MCaptureActivity::class.java), sCAN)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == sCAN && resultCode == RESULT_OK) {
            alert {
                title = "返回的二维码信息"
                customView {
                    scrollView {
                        verticalLayout {
                            textView {
                                text = data!!.getStringExtra("barCode")
                                setTextIsSelectable(true)
                            }
                            button {
                                text = "确定"
                                setOnClickListener { toast("确定") }
                            }
                            padding = dip(16)
                        }
                    }
                }

            }.show()
        }

    }

    //沉浸式状态栏
    private lateinit var immersionBar: ImmersionBar

    private fun initTitleBar(init: Boolean) {
        if (init) {
            immersionBar = ImmersionBar.with(this)
//                    .statusBarDarkFont(true)//黑色样式
            immersionBar.init()   //所有子类都将继承这些相同的属性
        } else {
            immersionBar.destroy()
        }
    }
}
